package com.huang825172.sbog.Tabs.Create.SelectPage.View;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huang825172.sbog.BaseFlux.Dispatcher;
import com.huang825172.sbog.BaseFlux.Store.IDataChangedListener;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Create.CapturePage.View.CapturePage;
import com.huang825172.sbog.Tabs.Create.SelectPage.Action.ActionCreator;
import com.huang825172.sbog.Tabs.Create.SelectPage.Store.Store;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.rosuh.filepicker.config.FilePickerManager;

public class SelectPage extends AppCompatActivity implements View.OnClickListener, IDataChangedListener {

    @BindView(R.id.SelectPageCaptureButton)
    Button captureButton;
    @BindView(R.id.SelectPageSelectButton)
    Button selectButton;

    @BindView(R.id.SelectPageInfoTextView)
    TextView infoTextView;

    @BindView(R.id.SelectPageTextEditText)
    EditText textEditText;
    @BindView(R.id.SelectPageTAGEditText)
    EditText tagEditText;

    @BindView(R.id.SelectPageUploadButton)
    Button uploadButton;

    @BindView(R.id.SelectPageRootView)
    ConstraintLayout rootView;

    private Store store;
    private ActionCreator actionCreator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_page);

        ButterKnife.bind(this);

        store = new Store(getApplicationContext());
        store.setDataChangedListener(this);
        actionCreator = new ActionCreator();
        Dispatcher.getSingleton().register(store);

        captureButton.setOnClickListener(this);
        selectButton.setOnClickListener(this);
        uploadButton.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0: {
                break;
            }
            case FilePickerManager.REQUEST_CODE: {
                if (resultCode == Activity.RESULT_OK) {
                    actionCreator.createGetVideoAction(FilePickerManager.INSTANCE.obtainData());
                }
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.SelectPageCaptureButton: {
                Intent intent = new Intent();
                intent.setClass(this, CapturePage.class);
                startActivityForResult(intent, 0);
                break;
            }
            case R.id.SelectPageSelectButton: {
                FilePickerManager.INSTANCE
                        .from(this)
                        .forResult(FilePickerManager.REQUEST_CODE);
                break;
            }
            case R.id.SelectPageUploadButton: {
                String text = textEditText.getText().toString();
                String tag = tagEditText.getText().toString();
                if (text.length() == 0 || tag.length() == 0) {
                    Snackbar.make(rootView, "请填写信息", Snackbar.LENGTH_SHORT).show();
                } else {
                    actionCreator.createCommitAction(text, tag);
                }
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void onDataChanged(String label) {
        switch (label) {
            case "SelectPageGetVideoAction": {
                infoTextView.setText(store.getVideoFilePaths().toString());
                break;
            }
            case "SelectPageFileUploaded":{
                Snackbar.make(rootView,"File Uploaded",Snackbar.LENGTH_SHORT).show();
                break;
            }
            case "SelectPageVideoPostSuccess":{
                Snackbar.make(rootView,"Upload Success",Snackbar.LENGTH_SHORT).show();
                break;
            }
            case "SelectPageVideoPostFail":{
                Snackbar.make(rootView,"Upload Fail",Snackbar.LENGTH_SHORT).show();
                break;
            }
            case "SelectPageFileExtError":{
                Snackbar.make(rootView, "请选择 MP4 文件", Snackbar.LENGTH_SHORT).show();
                break;
            }
            default:
                break;
        }
    }
}
